<?php

class Category extends MY_Controller {
    protected $table = 'tbl_category';
    protected $col_id = 'cate_id';
    protected $orders;
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('category_model');
        
    }
    public function index()
    {
        
        $data['list_cate'] = $this->category_model->show_cate(0);
        $this->layout('category/index', $data);
       
    }
    public function set_validate($id)
    {
        if(isset($id) && $id != '')
        {
            $this->form_validation->set_rules('txtname', 'Category name', 'required|is_unique[tbl_category.cate_name.cate_id.'.$id.']');  
        }
        else
        {
            $this->form_validation->set_rules('txtname', 'Category name', 'required|is_unique[tbl_category.cate_name]');
        } 
        $this->form_validation->set_message('required', '%s  is not empty');
        
    }
    public function get_data()
    {
        
        $cate_name = $this->input->post('txtname');
        $cate_parent = $this->input->post('parent');
        $order = 1;
        $list_cate = $this->category_model->list_tab('tbl_category');
        if(isset($list_cate) && is_array($list_cate))
        {
            $orders = array();
            foreach ($list_cate as $cate)
            {
                $orders[] = $cate['order'];
            }
            $order = max($orders) + 1;
        }
            
            $data = array(
                'cate_name' => $cate_name,
                'cate_parent' => $cate_parent,
                'order' => $order
            );
            $date_check = $this->check_form->check_sql($data);
            return $date_check;
    }

    public function insert()
    {
        $this->set_validate('');
        $data['list_cate'] = $this->category_model->show_cate(0);
        
        $data_add = $this->get_data();
        
        if($this->form_validation->run()=== FALSE)
        {   
            
            $this->layout('category/insert',$data);
        }
        else
        {
            
            $this->category_model->action_tab($this->table, $data_add, '');
            redirect('admin/category/index');  
        }
    }
    public function edit($id = '')
    {
        $this->set_validate($id);
        $data['list_cate'] = $this->category_model->show_cate(0);
        $data['get_one'] = $this->category_model->get_one($this->table, $this->col_id, $id);
       
       if(isset($id) && $id != '')
        {
            $data_edit = $this->get_data();
            $data_edit['order'] = $data['get_one'][0]['order'];
            $list_child = $this->category_model->show_cate($id);
            if($this->form_validation->run()=== FALSE)
            {
               $this->layout('category/edit',$data);
            }
            else
            {
                $flag = 0;
                foreach ($list_child as $child)
                    {
                        if($child['cate_id'] == $data_edit['cate_parent'])
                        {
                           $flag = 1;
                           $data['error'] = "Don't action. Please choose parent again";
                           $this->layout('category/edit',$data);
                        }
                    }
                if($flag == 0)
                {
                    $this->category_model->action_tab($this->table, $data_edit, $this->col_id,$id);
                    redirect('index.php/admin/category/index');
                }
                
            }
        }
    }
    
    public function delete($id = '')
    {
        if(isset($id) && $id != '')
        {
            $is_parent = $this->category_model->is_parent($id);
            
            if($is_parent === FALSE)
            {
                $this->category_model->detele_one($this->table, $this->col_id, $id); 
                
                redirect('admin/category/index'); 
            }
            else
            {
               // echo '<script>alert("no delete parent"); window.location="'.  base_url().'index.php/admin/category/index";</script>';
                redirect('admin/category/index'); 
            }
        }
    }
    public function up($id)
    {
        $parent = $this->category_model->get_info('cate_id', $id, 'cate_parent');
        $list_child = $this->category_model->childs($parent);
        if(isset($list_child) && is_array($list_child))
        {
            $orders = array();
            foreach ($list_child as $cate)
            {
                $orders[] = $cate['order'];
            }
        }
        //print_r($orders);
        sort($orders);
        $order_min = min($orders);
        $order_curent = $this->category_model->get_info('cate_id', $id, 'order');
        if($order_curent != $order_min)
        {
            foreach ($orders as $key => $order)
            {
                if($order == $order_curent)
                {
                    $order_move = $orders[$key - 1];

                }
            }
            $this->category_model->move($order_curent, $order_move);
        }
        
        redirect('admin/category/index'); 
    }
    public function down($id)
    {
        $parent = $this->category_model->get_info('cate_id', $id, 'cate_parent');
        $list_child = $this->category_model->childs($parent);
        if(isset($list_child) && is_array($list_child))
        {
            $orders = array();
            foreach ($list_child as $cate)
            {
                $orders[] = $cate['order'];
            }
        }
        //print_r($orders);
        sort($orders);
        $order_max = max($orders);
        $order_curent = $this->category_model->get_info('cate_id', $id, 'order');
        if($order_curent != $order_max)
        {
            foreach ($orders as $key => $order)
            {
                if($order == $order_curent)
                {
                    $order_move = $orders[$key + 1];

                }
            }
            $this->category_model->move($order_curent, $order_move);
        }
        
        redirect('admin/category/index'); 
    }
    
    
}

